package training_camp.day1.leetcode977;

class Solution {
    public int[] sortedSquares(int[] nums) {
        int l = 0;
        int r = nums.length - 1;
        int[] ans = new int[nums.length];
        int count = nums.length-1;
        while (l <= r) {
            int x1 = nums[l];
            int x2 = nums[r];
            if (x1*x1<x2*x2){
                ans[count] = x2*x2;
                r--;
            }
            else{
                ans[count] = x1*x1;
                l++;
            }
            count--;
        }
        return ans;
    }
}